﻿using System;
using System.Timers;

class AlarmClock
{
    private readonly System.Timers.Timer timer; // 定时器，每秒触发一次
    private DateTime alarmTime; // 目标闹钟时间
    private TimeSpan remainingTime; // 剩余时间

    // 事件委托
    public event EventHandler? Tick;   
    public event EventHandler? Alarm;  

    // 公共属性，用于获取剩余时间
    public TimeSpan RemainingTime => remainingTime;

    public AlarmClock()
    {
        timer = new System.Timers.Timer(1000); // 1秒触发一次
        timer.Elapsed += OnTick;
        timer.AutoReset = true; // 允许重复触发
    }

    // 设置闹钟（基于 TimeSpan）
    public void SetAlarm(TimeSpan duration)
    {
        alarmTime = DateTime.Now.Add(duration);
        remainingTime = duration;
        Console.WriteLine($"闹钟设定为 {alarmTime:HH:mm:ss}（倒计时 {remainingTime:c}）");
    }

    // 设置闹钟（基于具体时间）
    public void SetAlarm(DateTime time)
    {
        if (time <= DateTime.Now)
        {
            Console.WriteLine("错误：闹钟时间必须是未来时间！");
            return;
        }

        alarmTime = time;
        remainingTime = time - DateTime.Now;
        Console.WriteLine($"闹钟设定为 {alarmTime:HH:mm:ss}（倒计时 {remainingTime:c}）");
    }

    public void Start()
    {
        timer.Start();
        Console.WriteLine($"闹钟已启动，当前时间：{DateTime.Now:HH:mm:ss}");
    }

    public void Stop()
    {
        timer.Stop();
        Console.WriteLine("闹钟已停止。");
    }

    private void OnTick(object? sender, ElapsedEventArgs e)
    {
        remainingTime = alarmTime - DateTime.Now;

        // 触发 Tick 事件
        Tick?.Invoke(this, EventArgs.Empty);

        // 当时间到达或超过设定时间时触发闹钟
        if (DateTime.Now >= alarmTime)
        {
            Alarm?.Invoke(this, EventArgs.Empty);
            Stop();
        }
    }
}

class Program
{
    static void Main()
    {
        AlarmClock clock = new AlarmClock();
        clock.Tick += Clock_Tick;
        clock.Alarm += Clock_Alarm;

        Console.WriteLine("请选择闹钟模式：");
        Console.WriteLine("1. 按秒倒计时");
        Console.WriteLine("2. 设定具体时间");
        Console.Write("请输入模式（1 或 2）：");

        string? mode = Console.ReadLine();

        void mode1()
        {
            Console.Write("请输入倒计时秒数：");
            if (int.TryParse(Console.ReadLine(), out int seconds) && seconds > 0)
            {
                TimeSpan duration = TimeSpan.FromSeconds(seconds);
                clock.SetAlarm(duration);
                clock.Start();
            }
            else
            {
                Console.WriteLine("请输入正确的秒数。");
                mode1();
            }
        }

        void mode2()
        {
            Console.Write("请输入闹钟时间（格式 英文输入法 小时:分钟:秒钟）：");
            if (DateTime.TryParse(Console.ReadLine(), out DateTime alarmDateTime) && alarmDateTime > DateTime.Now)
            {
                clock.SetAlarm(alarmDateTime);
                clock.Start();
            }
            else
            {
                Console.WriteLine("请输入正确的时间，且必须是未来时间。");
                mode2();
            }
        }

        if (mode == "1")
        {
            mode1();
        }
        else if (mode == "2")
        {
            mode2();
        }
        else
        {
            Console.WriteLine("无效输入，默认选择: 1. 按秒倒计时");
            mode1();
        }

        // 保持程序运行，防止主线程退出
        while (true) { }
    }

    private static void Clock_Tick(object? sender, EventArgs e)
    {
        AlarmClock clock = (AlarmClock)sender!;
        // 获取剩余时间
        TimeSpan remainingTime = clock.RemainingTime;

        // 获取剩余的秒数并向上取整
        int remainingSeconds = (int)Math.Ceiling(remainingTime.TotalSeconds);

        // 输出当前时间和剩余时间
        Console.WriteLine($"当前时间: {DateTime.Now:HH:mm:ss} (剩余 {remainingSeconds} 秒)");
    }

    private static void Clock_Alarm(object? sender, EventArgs e)
    {
        Console.WriteLine("闹钟响了！！");
        Environment.Exit(0);
    }
}
